﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;

namespace CourseSystem.Stat
{
    public class UserOnlineStat
    {
        private static readonly List<UserOnlineInfo> OnlineList;

        static UserOnlineStat()
        {
            OnlineList = new List<UserOnlineInfo>();
        }

        public static void Stat()
        {
            var now = DateTime.Now;
            var user = UserHelper.LoginUser;
            if (user == null || user.UserType == 0)
            {
                var ip = CommonRequest.GetIP();
                var info = OnlineList.FirstOrDefault(o => o.UserId == 0 && o.UserIp == ip && o.UserType == 0);
                if (info == null)
                {
                    OnlineList.Add(new UserOnlineInfo { UserType = 0, UserIp = ip, UserId = 0, LastUpdateTime = now, StartTime = now });
                }
                else
                    info.LastUpdateTime = now;
                return;
            }
            var userInfo = OnlineList.FirstOrDefault(o => o.UserId == user.Id && o.UserType == (byte)user.UserType);
            if (userInfo == null)
            {
                OnlineList.Add(new UserOnlineInfo { UserType = (byte)user.UserType, UserIp = "", UserId = user.Id, LastUpdateTime = now, StartTime = now });
            }
            else
                userInfo.LastUpdateTime = now;
        }

        public static void Remove(int id, int type)
        {
            OnlineList.RemoveAll(o => o.UserId == id && o.UserType == type);
        }

        public static int GetOnlineCount()
        {
            return OnlineList.Count;
        }

        public static void ClearOnline()
        {
            //TODO,暂定一分钟,以后设置为2.15
            //1分钟无活动则清理
            foreach (var item in OnlineList)
            {
                if (item.LastUpdateTime.AddMinutes(1) < DateTime.Now)
                {
                    if (item.UserId != 0)
                    {
                        //学生
                        if (item.UserType == 1)
                        {
                            StudentBLL.UpdateOnlineTime(item.UserId, (item.LastUpdateTime - item.StartTime).Minutes);
                        }
                        //老师
                        if (item.UserType == 2)
                        {
                            TeacherBLL.UpdateOnlineTime(item.UserId, (item.LastUpdateTime - item.StartTime).Minutes);
                        }
                    }
                }
            }
            OnlineList.RemoveAll(o => o.LastUpdateTime.AddMinutes(1) < DateTime.Now);

        }

        public static void CommitCurrent()
        {
            var user = UserHelper.LoginUser;
            var find = OnlineList.FirstOrDefault(o => o.UserType == (byte) user.UserType && o.UserId == user.Id);
            if (find == null)
                return;
            if (find.UserId != 0)
            {
                //学生
                if (find.UserType == 1)
                {
                    StudentBLL.UpdateOnlineTime(find.UserId, (find.LastUpdateTime - find.StartTime).Minutes);
                }
                //老师
                if (find.UserType == 2)
                {
                    TeacherBLL.UpdateOnlineTime(find.UserId, (find.LastUpdateTime - find.StartTime).Minutes);
                }
                //清理
                Remove(find.UserId, find.UserType);
            }
        }
    }
}
